package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Housing;
import com.woniuxy.entity.Owner;
import com.woniuxy.mapper.HousingMapper;
import com.woniuxy.mapper.OwnerMapper;
import com.woniuxy.service.OwnerService;
import com.woniuxy.util.PageData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@AllArgsConstructor
public class OwnerServiceImpl implements OwnerService {
    private OwnerMapper ownerMapper;
    private HousingMapper housingMapper;

    @Override
    public PageInfo<Owner> queryOwner(PageData<Owner> pageData) {
        //开始分页PageNum当前页数     PageSize每页的个数
        PageHelper.startPage(pageData.getPageNum(), pageData.getPageSize());
        List<Owner> owners = ownerMapper.queryOwner(pageData.getParam());
        PageInfo pageInfo = new PageInfo(owners);
        return pageInfo;
    }

    //写入业主
    //把选中的房屋改为1，已入住
    @Override
    public void saveOwner(Owner owner) {

        ownerMapper.addOwner(owner);
        if(owner.getHouseId()!=null){
            Housing housing = new Housing();
            housing.setId(owner.getHouseId());
                        //使用sql语句中的方法拿到的id
            housing.setOwnerId(owner.getId());
            housingMapper.updateHouseState(housing);
        }


    }

    @Override
    public boolean batchDelOwner(List<Integer> ids) {
        for (Integer id : ids) {
            List<Owner> owners = ownerMapper.queryDetailOwner(id);
            for (Owner owner : owners) {
                if(owner.getStatus()==1 && owner.getHouseName()!=null){
                    return false;
                }
            }
        }
        return ownerMapper.batchDelOwner(ids);
    }

    @Override
    public Owner queruOwnerById(Integer id) {
        return ownerMapper.queruOwnerById(id);
    }

    @Override
    public void saveUpdate(Owner owner) {
        ownerMapper.saveUpdate(owner);
    }

    @Override
    public boolean saveDelOwner(Integer id) {
        List<Owner> owners = ownerMapper.queryDetailOwner(id);
        for (Owner owner : owners) {
            if(owner.getStatus()==1 && owner.getHouseName()!=null){
                return false;
            }
        }
        return ownerMapper.saveDelOwner(id);
    }

    @Override
    public List<Owner> queryDetailOwner(Integer id) {
        return ownerMapper.queryDetailOwner(id);
    }

    @Override
    public Owner queryDetailById(Integer id) {
        return ownerMapper.queryDetailById(id);
    }
}
